//
//  ViewController.swift
//  MeunDemo
//
//  Created by hfzhangzhang on 2018/3/19.
//  Copyright © 2018年 hfzhangzhang. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let lab:ZZMeunLabel = ZZMeunLabel.init(frame: CGRect.init(x: 0, y: 20, width: 360, height: 100))
        lab.numberOfLines = 0
        lab.customMeunItemOptions = [.UICustomMeunItem_copy, .UICustomMeunItem_unSelect]
        lab.text = "这个菜单就是UIMenuController，系统默认支持UITextField、UITextView、UIWebView控件的UIMenuController相关操作,对于系统不支持UIMenuController操作的控件，我们就要自定义控件的UIMenuController来实现相关功能"
        lab.customSizeToFit()
        lab.textColor = UIColor("123333")
        self.view.addSubview(lab)
        
        
        let view = UIViewEffect.init(frame: CGRect.init(x: 10, y: 120, width: 200, height: 200))
        view.backgroundColor = UIColor("666666")
        view.effectViewWithAlpha(alpha: 1)
        view.layer(radius: 5, borderWidth: 1, borderColor: .red)
        view.addTapGestureRecognizer(target: self, action: #selector(funtext))
        view.selectColor = .yellow
        self.view.addSubview(view)
        
        
        let tf = UIPlaceholderTextView(frame: CGRect.init(x: 200, y: 200, width: 200, height: 300))
        tf.tintColor = .red;
        //tf.backgroundColor = UIColor("666666")
        tf.placeholder = "qingshuruingshuruingshuruingshuruingshuruingshuruingshuru"
        tf.layer(radius: 5, borderWidth: 1, borderColor: .red)
        tf.frame = CGRect(x: 200, y: 100, width: 100, height: 100)
        self.view.addSubview(tf)
        
  
    }
       @objc func funtext() -> () {
        print("22")
        
    }
    
}

private var placeholderkey: String?
private var placeholderLablekey: UILabel?

extension UITextView{
//    private  var placeholderLable: UILabel?{
//        get {
//            return (objc_getAssociatedObject(self, &placeholderLablekey) as? UILabel)
//        }
//        set(newValue) {
//            objc_setAssociatedObject(self, &placeholderLablekey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
//        }
//    }
//    var placeholder: String? {
//        get {
//            return (objc_getAssociatedObject(self, &placeholderkey) as? String)
//        }
//        set(newValue) {
//            objc_setAssociatedObject(self, &placeholderkey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
//            if let value = newValue, !value.isEmpty {
//                self.placeholderLable = UILabel(frame: CGRect(x: 5, y: 5, width: self.width-10, height: self.height-10))
//                self.placeholderLable?.font = self.font
//                self.placeholderLable?.text = value
//                self.placeholderLable?.textColor = UIColor("999999")
//                self.addSubview(self.placeholderLable!)
//            }
//        }
//    }
}

class UIPlaceholderTextView: UITextView {
    private  var placeholderLable: UILabel?
    
    override var frame: CGRect {

        didSet {
            print("storeProperty did Set")
             self.placeholderLable?.customSizeToFit()
        }
        
    }
    
    var placeholder: String? {
        get{
            return self.placeholder
        }
        set(newValue) {
            if let value = newValue, !value.isEmpty {
                self.placeholderLable = UILabel(frame: CGRect(x: 5, y: 5, width: self.width-10, height: self.height-10))
                self.placeholderLable?.numberOfLines = 0;
                self.placeholderLable?.font = self.font
                self.placeholderLable?.text = value
                self.placeholderLable?.textColor = UIColor("999999")
                self.placeholderLable?.customSizeToFit()
                self.addSubview(self.placeholderLable!)
                NotificationCenter.default.addObserver(self, selector:#selector(textViewTextDidChange), name: .UITextViewTextDidChange, object: nil)
            }
    }
    }
    @objc func textViewTextDidChange() -> () {
        if let text = self.text, !text.isEmpty {
            self.placeholderLable?.isHidden = true
        }
        else {
            self.placeholderLable?.isHidden = false
        }
    }
    deinit {
          NotificationCenter.default.removeObserver(self)
    }
}

